package com.example.emailWeb.Socket.Proxy.ok;

import com.example.emailWeb.Socket.Proxy.PolySocketData;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9993);
        while(true){
            Socket accept = serverSocket.accept();
            PolySocketData dataBuff = recPolyDataBuff(accept);
            System.out.println(dataBuff);
        }
    }

    public static PolySocketData recPolyDataBuff(Socket socket) throws IOException {
        ObjectInputStream ois = null;
        ObjectOutputStream os = null;
        PolySocketData polySocketData = null;
        try {
            ois = new ObjectInputStream(socket.getInputStream());
            polySocketData = (PolySocketData) ois.readObject();
            os =  new ObjectOutputStream(socket.getOutputStream());
            os.writeObject(polySocketData);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ois!=null){
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            if(os!=null){
                os.close();
            }
            if(socket!=null)
                socket.close();
        }
        return polySocketData;
    }
}
